﻿#!/usr/bin/env 
#-*- coding:utf-8 -*-

import  web,os,model,sqlite3
from datetime import datetime
from jinja2 import Environment,FileSystemLoader
        
urls = (
	'/(.*)/', 'redirect', 
    '/', 'index',
    '/index', 'index',
 	'/favicon.ico', 'icon',
 	'/books','books',
 	'/management','management',
 	'/aboutme','aboutme',
 	'/contactme','contactme',
 	'/login','login',
    
)

def render_template(template_name, **context):
    extensions = context.pop('extensions', [])
    globals = context.pop('globals', {})

    jinja_env = Environment(
            loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')),
            extensions=extensions,
            )
    jinja_env.globals.update(globals)

    #jinja_env.update_template_context(context)
    return jinja_env.get_template(template_name).render(context)

class index:        
    def GET(self):
        return render_template('index.html',name='hello')

class redirect(object):
	"""docstring for redirect"""
	def GET(self,path):
		print(path)   
		web.seeother('/' + path) 

class icon:
    def GET(self): return web.seeother("/static/images/favicon.ico")

class books(object):
	"""docstring for ClassName"""
	def GET(self):
		# model.new_post('123','1','123',u'234abc你好',datetime.now())
		posts = list(model.get_posts())
		for post in posts:
			post.RDateTime = datetime.strptime(posts[0].RDate[0:19], "%Y-%m-%d %H:%M:%S")
			web.debug(post.RDate)
		return render_template('books.html', posts = posts)

class management(object):
	"""docstring for ClassName"""
	def GET(self):
		return render_template('management.html')

class aboutme(object):
	"""docstring for ClassName"""
	def GET(self):
		return render_template('aboutme.html')

class contactme(object):
	"""docstring for ClassName"""
	def GET(self):
		return render_template('contactme.html')

class login(object):
	def GET(self):
		pass
	def POST(self):
		rememberme = 'off'
		form = web.input()
		email = form.email
		password = form.password

		if 'rememberme' in form:
			rememberme = form.rememberme
		
		return (email,password,rememberme)
		
		
app = web.application(urls, globals())
if __name__ == "__main__":

    app.run()